package team.tunan.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import team.tunan.common.LayUIResult;
import team.tunan.common.Result;
import team.tunan.entity.Score;
import team.tunan.entity.dto.MatchInfo;
import team.tunan.service.IMatchesService;
import team.tunan.service.IScoreService;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sqx
 * @since 2024-01-01
 */
@Controller
//@RequestMapping("/score")
@RequestMapping("score")
public class ScoreController {

    @Autowired
    private IScoreService scoreService;

    @Autowired
    private IMatchesService matchService;


    @RequestMapping("/page")
    public String score(Model model){
//        MatchInfo matchToShow = matchService.getMatchToShow();
//        model.addAttribute("info",matchToShow);
        return "scoreList";
    }
    @RequestMapping("/pageShow/{mid}")
    @ResponseBody
    public ModelAndView scoreShow(@PathVariable Integer mid, Model model, HttpServletResponse response){
        MatchInfo matchToShow = matchService.getMatchToShow(mid);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("info",matchToShow);
        modelAndView.setViewName("score");
        return modelAndView;
    }


    /**
     * 去打分
     * @param score
     * @return
     */
    @RequestMapping("/doScore")
    @ResponseBody
    public Result doScore(Score score){

        Map<String,Object> map = scoreService.doScore(score);
        boolean flag =(boolean) map.get("flag");
        return new Result(flag?"打分成功!":"你已经打过分了,不需要再点了哟!",map,flag);
    }

    /**
     *去排行榜界面
     * @return
     */
    @RequestMapping("/toRanking")
    public String toRanking(){
        return "ranking.html";
    }


    /**
     * 刚进页面全部加载
     * @param score
     * @return
     */
    @RequestMapping("/ranking")
    @ResponseBody
    public LayUIResult ranking(Score score){
        List<Map<String,Object>> map = scoreService.getRankingList();
        long count = map.stream().count();
        return new LayUIResult(0,null,count,map);
    }


    /**
     * 根据比赛查找哪些可以打分
     * @param gid
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public LayUIResult getList(Integer gid){
        List<Map<String,Object>> map = scoreService.getStartedList(gid);
        long count = map.stream().count();
        return new LayUIResult(0,null,count,map);
    }

    /**
     * 查找所有可以打分的场次
     * @return
     */
    @RequestMapping("/listAll")
    @ResponseBody
    public LayUIResult getListAll(){
        List<Map<String,Object>> map = scoreService.getStartedList2();
        long count = map.stream().count();
        return new LayUIResult(0,null,count,map);
    }


    /**
     * 根据比赛id和轮数查看选手的成绩
     * @param gid
     * @param roundNum
     * @return
     */
    @RequestMapping("/ranking2")
    @ResponseBody
    public LayUIResult ranking2(Integer gid, Integer roundNum){
        System.out.println(gid);
        System.out.println(roundNum);
        System.out.println("=============");
        System.out.println("=============");
        System.out.println("=============");
        List<Map<String,Object>> map = scoreService.getRankingList2(gid, roundNum);
        long count = map.stream().count();
        return new LayUIResult(0,null,count,map);
    }
}

